﻿using System;
using System.Data;
using DAL;

namespace PeisongWeb.admin
{
    public partial class myCustomerPriceStructure : adminPage
    {
        protected int companyId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["companyId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["companyId"] = value; }
        }



        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (hasPermission("IB", 1))
                {
                    if (Request.QueryString["company"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["company"]))
                        {
                            companyId = Convert.ToInt32(Request.QueryString["company"]);
                            
                        }
                    }

                    rptBind();
                    if (isTestCompany(companyId))
                    {
                        
                        bt_save.Visible = true;
                    }
                    else
                    {
                        //Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                        //return;
                        bt_save.Visible = false;

                    }

                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }

        }
        //判读是否为测试企业
        bool isTestCompany(int _comId)
        {
            CompanyEntity com = logic.company.getById(_comId);
            if (com.salesperson == logic.sysAdmin.AdminID.ToString() && com.buyerType == "2")
                return true;
            else
                return false;
        }

        private void rptBind()
        {
            lbl_CompanyName.Text = logic.company.getById(companyId).Company;
            DataTable dt = logic.category.listWithNewParentByCompany(companyId);
            rpt_list.DataSource = dt;
            rpt_list.DataBind();
        }

        protected void bt_save_Click(object sender, EventArgs e)
        {
            string[] splitstr = { "," };
            string[] strs = hd_pricePercent.Value.Split(splitstr, StringSplitOptions.RemoveEmptyEntries);

            //删除原来价格体系
            logic.priceStructure.deleteByCompany(companyId);

            for (int i = 0; i < strs.Length; i = i + 2)
            {
                int categoryId = Convert.ToInt32(strs[i + 1]);
                DataTable productDt = logic.product.getByCategory(categoryId);

                foreach (DataRow dr in productDt.Rows)
                {
                    PriceStructureEntity ps = new PriceStructureEntity();
                    ps.CategoryId = categoryId;
                    ps.ProductId = Convert.ToInt32(dr["productId"]);
                    ps.CompanyId = companyId;
                    ps.AutoPercent = Convert.ToDecimal(strs[i]);

                    logic.priceStructure.insert(ps);  //加入价格体系

                    logic.buyerPriceHistory.insertByUpdateBuyerPriceStuctrue(ps.ProductId, companyId); //增加采购商历史价格
                }
            }


            Library.Script.ClientMsgUrl("企业价格体系初始化成功!", "companyList.aspx");

        }

        protected void lbn_Import_Click(object sender, EventArgs e)
        {
            DataTable dt = logic.product.listWithPriceByBuyer(companyId);
            //调用xlsToExcel方法生成Excel报表
        }
    }
}
